package com.apache.ius.interceptor;

import com.apache.ius.common.annotion.BeanFactory;
import com.apache.ius.common.annotion.SpringHandlerInterceptor;
import net.sf.json.JSONObject;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Map;

/**
 * description: 业务请求拦截器
 *
 * @author dyh 创建时间：2019年02月11日
 */
@SpringHandlerInterceptor(includePatterns = { "/trans/info/*", "/trans/list/*", "/trans/submit/*"})
public class TransInterceptor extends SuperInterceptor {

    @Override
    public boolean exeHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler) throws Exception {
        String uri = request.getRequestURI();
        String tokenId = request.getParameter("token");//用于获取登录用户信息
        Map<String, String> params = getParameterMap(request);
        if (uri.indexOf("/trans/submit/") != -1) {
//            LoginUser loginUser = null;
//            if (ToolsUtil.isEmpty(loginUser)) {
//                request.setAttribute("errorCode", BeanFactory.NOT_LOGIN);//没登录
//                postHandle(request, response, handler, null);
//                return false;
//            }
        }
        request.setAttribute("requestParams", params);
        return true;
    }

    public boolean bodyHandle(HttpServletRequest request, HttpServletResponse response,
            Object handler, String message) throws Exception {
        Map<String, Object> params = this.getParameterMap(message);
        String uri = request.getRequestURI();
        String tokenId = request.getParameter("token");//用于获取登录用户信息
        if (uri.indexOf("/trans/submit/") != -1) {
            //            LoginUser loginUser = null;
            //            if (ToolsUtil.isEmpty(loginUser)) {
            //                request.setAttribute("errorCode", BeanFactory.NOT_LOGIN);//没登录
            //                postHandle(request, response, handler, null);
            //                return false;
            //            }
        }
        request.setAttribute("requestParams", params);
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest request, HttpServletResponse response, Object o,
            ModelAndView modelAndView) throws Exception {
        response.setContentType(request.getContentType());
        String errorCode = String.valueOf(request.getAttribute("errorCode"));
        String msg = null;
        if(BeanFactory.NOT_LOGIN.equals(errorCode)){//没有登录的操作
            JSONObject array = this.gotoLogin(request,response,"");
            if(null == array){
                this.sendLogin(request,response);
                return ;
            }
            msg = array.toString();
        } else{
            //返回的数据信息
            Object obj = request.getAttribute(BeanFactory.RETURN_MSG_KEY);
            msg = String.valueOf(obj);
            request.removeAttribute(BeanFactory.RETURN_MSG_KEY);
        }
        outputJson(msg, response, request.getParameter("callback"));
    }
}
